package com.pansoft.openplanet.util;

import android.util.Log;

import com.efounder.constant.EnvironmentVariable;
import com.efounder.http.EFHttpRequest;
import com.utilcode.util.LogUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;

import retrofit2.http.DELETE;

/**
 * 网络请求类
 * Created by will on 18-6-8.
 */

public class StarAssetRequestUtil {

    public static final String BASE_URL = EnvironmentVariable.getProperty("TalkChainUrl") + "/tcserver";

    private static final String TAG = "StarAssetRequestUtil";

    /**
     * 通用get请求
     *
     * @param tag       tag
     * @param method    url: 传入"path/method"
     * @param paramsMap 参数
     * @param callback  callback
     */
    @Deprecated
    public static void getCommonRequest(String tag, String method, HashMap<String, String> paramsMap, final SARequestCallback callback) {
        StringBuilder tempParams = new StringBuilder();
        try {
            int pos = 0;
            for (String key : paramsMap.keySet()) {
                if (pos > 0) {
                    tempParams.append("&");
                }
                tempParams.append(String.format("%s=%s", key, URLEncoder.encode(paramsMap.get(key), "utf-8")));
                pos++;
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String requestUrl = String.format("%s/%s?%s", BASE_URL, method, tempParams.toString());
        LogUtils.d(requestUrl);
        EFHttpRequest httpRequest = new EFHttpRequest(tag);
        httpRequest.setHttpRequestListener(new EFHttpRequest.HttpRequestListener() {
            @Override
            public void onRequestSuccess(int requestCode, String response) {
                callback.onSuccess(response);
            }

            @Override
            public void onRequestFail(int requestCode, String message) {
                callback.onFail(message);
            }
        });
        Log.d("TCGetRequest", requestUrl);
        httpRequest.httpGet(requestUrl);
    }
    /**
     * 通用get请求
     *
     * @param tag       tag
     * @param requestrUrl    url: 传入"完整的url
     * @param paramsMap 参数
     * @param callback  callback
     */
    public static void getRequest(String tag, String requestrUrl, HashMap<String, String> paramsMap, final SARequestCallback callback) {
        StringBuilder tempParams = new StringBuilder();
        try {
            int pos = 0;
            for (String key : paramsMap.keySet()) {
                if (pos > 0) {
                    tempParams.append("&");
                }
                tempParams.append(String.format("%s=%s", key, URLEncoder.encode(paramsMap.get(key), "utf-8")));
                pos++;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        String requestUrl = String.format("%s?%s",  requestrUrl, tempParams.toString());
        LogUtils.d(requestUrl);
        EFHttpRequest httpRequest = new EFHttpRequest(tag);
        httpRequest.setHttpRequestListener(new EFHttpRequest.HttpRequestListener() {
            @Override
            public void onRequestSuccess(int requestCode, String response) {
                callback.onSuccess(response);
            }

            @Override
            public void onRequestFail(int requestCode, String message) {
                callback.onFail(message);
            }
        });
        Log.d("TCGetRequest", requestUrl);
        httpRequest.httpGet(requestUrl);
    }

    /**
     * 通用post方法请求
     *
     * @param tag       class name
     * @param method    url: 传入"path/method"
     * @param paramsMap 参数
     * @param callback  callback
     */
    @Deprecated
    public static void postCommonRequest(final String tag, String method, HashMap<String, String> paramsMap, final SARequestCallback callback) {
        EFHttpRequest httpRequest = new EFHttpRequest(tag);
        httpRequest.setHttpRequestListener(new EFHttpRequest.HttpRequestListener() {
            @Override
            public void onRequestSuccess(int requestCode, String response) {
                if (callback != null) {
                    if (response != null)
                        Log.i(tag, response);
                    callback.onSuccess(response);
                }
            }

            @Override
            public void onRequestFail(int requestCode, String message) {
                if (callback != null) {
                    if (message != null)
                        Log.e(tag, message);
                    callback.onFail(message);
                }
            }
        });
        httpRequest.httpPost(String.format("%s/%s", BASE_URL, method), paramsMap);
    }


    public interface SARequestCallback {

        void onSuccess(String response);

        void onFail(String error);

    }

}